package com.game.modular.gameCore.battle.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.activerecord.Model;

import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

/**
 * 战斗公式(CoreCombatFormula)表实体类
 *
 * @author cxy
 * @since 2024-08-06 20:28:01
 */
@ApiModel("战斗公式")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
@SuppressWarnings("serial")
public class CoreCombatFormula extends Model<CoreCombatFormula> {// swagger2,mybatis-plus
    ///////////////////////////////////////////////////////////////////////////////////////////////////////// 生成实体字段
    @TableId(type = IdType.AUTO)
    @ApiModelProperty("公式id")
    private Long id;

    @ApiModelProperty("公式描述")
    private String text;

    @ApiModelProperty("公式返回字段")
    private String back;

    @ApiModelProperty("公式名字")
    private String name;

    @ApiModelProperty("公式系数")
    private String value;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////// 序列化版本号
    @TableField(exist = false)
    private static final long serialVersionUID = 334989687522570104L;

    @TableField(exist = false)
    @ApiModelProperty("公式系数")
    private double[] values;

    /////////////////////////////////////////////////////////////////////////////////////////////////////////// 获取主键值
    @Override
    public Serializable pkVal() {
        return this.id;
    }

    public double[] valueToValues() {
        if (value == null){
            return new double[0];
        }
        if (value.contains("|")) {
            String[] values = value.split("\\|");
            double[] result = new double[values.length];
            for (int i = 0; i < values.length; i++) {
                String val = values[i];
                if (val.contains("-")) {
                    String[] range = val.split("-");
                    if (range.length != 2) {
                        throw new IllegalArgumentException("Invalid range format: " + val);
                    }
                    long start = Long.parseLong(range[0]);
                    long end = Long.parseLong(range[1]);
                    result[i] = (Math.random() * (end - start) + start) / 100.00;
                } else {
                    result[i] = Double.parseDouble(val) / 100.00;
                }
            }
            return result;
        } else {
            if (value.contains("-")) {
                String[] range = value.split("-");
                if (range.length != 2) {
                    throw new IllegalArgumentException("Invalid range format: " + value);
                }
                long start = Long.parseLong(range[0]);
                long end = Long.parseLong(range[1]);
                double[] result = new double[1];
                result[0] = (Math.random() * (end - start) + start) / 100.00;
                return result;
            } else {
                double[] result = new double[1];
                result[0] = Double.parseDouble(value) / 100.00;
                return result;
            }
        }
    }
}

